package com.dimples.dd.system.controller;

import com.dimples.dd.common.constant.DDServerConstant;
import com.dimples.dd.common.model.Option;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.common.result.CommonResult;
import com.dimples.dd.log.core.annotation.ApiLog;
import com.dimples.dd.log.core.enums.OperateTypeEnum;
import com.dimples.dd.system.model.form.RoleForm;
import com.dimples.dd.system.model.query.RolePageQuery;
import com.dimples.dd.system.model.vo.RolePageVO;
import com.dimples.dd.system.service.SystemRoleService;
import com.dimples.dd.web.core.annotation.PreventDuplicateResubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/7/3
 */
@Tag(name = "02.角色接口")
@RestController
@Validated
@RequestMapping("roles")
@RequiredArgsConstructor
public class SysRoleController {

    private final SystemRoleService systemRoleService;

    @Operation(summary = "角色分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('system:role:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM)
    public PageResult<RolePageVO> getRolePage(
            @ParameterObject RolePageQuery queryParams
    ) {
        return systemRoleService.getRolePage(queryParams);
    }

    @Operation(summary = "角色下拉列表")
    @GetMapping("/options")
    @PreAuthorize("@ss.hasPerm('system:role:query')")
    public CommonResult<List<Option<String>>> listRoleOptions() {
        List<Option<String>> list = systemRoleService.listRoleOptions();
        return CommonResult.success(list);
    }

    @Operation(summary = "角色表单数据")
    @GetMapping("/{roleId}/form")
    @PreAuthorize("@ss.hasPerm('system:role:query')")
    @ApiLog(operateType = OperateTypeEnum.SELECT, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<RoleForm> getRoleForm(
            @Parameter(description = "角色ID") @PathVariable Long roleId
    ) {
        RoleForm roleForm = systemRoleService.getRoleForm(roleId);
        return CommonResult.success(roleForm);
    }

    @Operation(summary = "新增角色")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('system:role:create')")
    @PreventDuplicateResubmit
    @ApiLog(operateType = OperateTypeEnum.CREATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> createRole(@Valid @RequestBody RoleForm roleForm) {
        boolean result = systemRoleService.createRole(roleForm);
        return CommonResult.success(result);
    }

    @Operation(summary = "修改角色状态")
    @PatchMapping(value = "/{roleId}/status")
    @PreAuthorize("@ss.hasPerm('system:role:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateRoleStatus(
            @Parameter(description = "角色ID") @PathVariable Long roleId,
            @Parameter(description = "状态(1:启用;0:禁用)") @RequestParam @NotNull(message = "不能为空") Integer status
    ) {
        boolean result = systemRoleService.updateRoleStatus(roleId, status);
        return CommonResult.success(result);
    }

    @Operation(summary = "修改角色")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('system:role:update')")
    @ApiLog(operateType = OperateTypeEnum.UPDATE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> updateRole(@Valid @RequestBody RoleForm roleForm) {
        boolean result = systemRoleService.updateRole(roleForm);
        return CommonResult.success(result);
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('system:role:delete')")
    @ApiLog(operateType = OperateTypeEnum.DELETE, applicationName = DDServerConstant.DD_SERVER_SYSTEM, responseEnable = true)
    public CommonResult<Boolean> deleteRoles(
            @Parameter(description = "删除角色，多个以英文逗号(,)分割") @NotNull(message = "不能为空") @PathVariable String ids
    ) {
        boolean result = systemRoleService.deleteRoles(ids);
        return CommonResult.success(result);
    }
}
